package admin

import (
	"context"

	"gitee.com/linxing_3/sr-core/config/source"
	"gitee.com/linxing_3/sr-core/domain"
	"gitee.com/mkwchecking/sr-admin/apps/admin/adapter/http"
	"gitee.com/mkwchecking/sr-admin/apps/admin/conf"
	"gitee.com/mkwchecking/sr-admin/apps/admin/entity"
	"gitee.com/mkwchecking/sr-admin/pkg/plugins/pdatabase"
)

var _ domain.IDomain = (*SysDomain)(nil)

func NewSysDomain() domain.IDomain {
	return &SysDomain{}
}

type SysDomain struct {
}

// Install implements domain.IDomain.
func (a *SysDomain) Install(ctx context.Context, src source.ISource) error {
	var err error
	conf.SetupConfig(src)

	if err = http.InitHttp(ctx); err != nil {
		return err
	}

	return err
}

// OnSourceChange implements domain.IDomain.
func (a *SysDomain) BeforeStart(src source.ISource) error {

	db, err := pdatabase.GetDatabaseE()
	if err != nil {
		return err
	}

	if err := entity.AutoMigrate(db); err != nil {
		return err
	}

	return nil
}

// Start implements domain.IDomain.
func (a *SysDomain) Start(ctx context.Context) error {
	return http.OnStart(ctx)
}

// Stop implements domain.IDomain.
func (a *SysDomain) Stop(ctx context.Context) error {
	return nil
}
